Creating modal forms
During my trainings and
working with Dynamics AX users, I noticed that people who are not
familiar with computers and software tend to get lost among open
application windows. The same could be applied to Dynamics AX. I
experienced many times when a user opened one form, clicked some button
to open another one, and then went back to the first one without closing
the second one. Sometimes this happens intentionally,
sometimes— not, but the result is that the second form is
hidden behind the first one and the user starts wondering why it is not
possible to close or edit the first form.
Such issues could be
easily solved by making the child form a modal window. In other words,
the second form always stays on top of the first one until closed. In
this recipe, we will do exactly that. As an example, we will make the Create sales order form a modal window.
How to do it...
1. Open the SalesCreateOrder form in AOT, and set its Design property:
Property
|
Value
|
---|
WindowType
|
Popup
|
2. To test, open Accounts receivable | Sales Order Details, and start creating a new order. Notice that now the sales order creation form always stays on top of the Sales order form:
How it works...
Dynamics AX form design has a WindowType property, which is set to Standard by default. In order to make a form behave as a modal window, we have to change it to Popup. Such forms will always stay on top of the parent form.
There's more...
We already know that some of the Dynamics AX forms are created dynamically using the Dialog class. If we look deeper into the code, we could find that the Dialog
class actually creates a runtime Dynamics AX form. That means we can
apply the same principle, i.e. change the relevant form's design
property. The following code could be added to the Dialog object and would do the job:
dialog.dialogForm().buildDesign().windowType(
FormWindowType::Popup);
We get a reference to the form's design, by first using dialogForm() of the Dialog object to get a reference to the DialogForm object, and then we call buildDesign() on the latter object. Then, we set the design's property by calling its windowType() with an argument FormWindowType::Popup.Changing common form appearance
In every single
multi-company Dynamics AX project, in order to prevent user mistakes, I
was asked to add functionality that allows setting the background color
of every form per company. By doing that, users clearly see in which
company account they are at the moment and can easily work within
multiple companies at the same time.
In this recipe, we will modify SysSetupFormRun class to change the background color for every form in Dynamics AX.
How to do it...
1. Open SysSetupFormRun in AOT, and override its run() with the following code:
public void run()
{;
super();
this.design().colorScheme(FormColorScheme::RGB);
this.design().backgroundColor(WinAPI::RGB2int(255,0,0));
}
2. To test the results, open any Dynamics AX form, for example, General ledger | Chart of Accounts Details and notice how the background color is changed to red:
How it works...
SysSetupFormRun is the application
class that is called by the system every time a user runs a form. The
best place to add our custom code is to override the run() method and place it under the super().
We use this.design() to get a reference to the form's design. By calling colorScheme() and backgroundColor(), we set the color scheme to red/green/blue and the color code to red.
We use WinAPI::RGB2int() to transform the human-readable red/green/blue code into the numeric color code.
There's more...
This recipe showed a very
basic principle of how to change the common appearance of all forms with
few lines of code. You noticed that the color in this recipe does not
fill all areas of the form, which does not make the form look nice. An
alternative to this could be to dynamically add a colored rectangle or
something similar to the top of the form. The possibilities are endless
here. New controls like input fields, buttons, menu items, and others
could also be added to all forms dynamically using this class. But do
not overdo as it may impact system performance.
For example, this
recipe could be combined with one of the later recipes, Picking a color
from Chapter 4, Building Lookups. In this recipe, one color could be set
per each Dynamics AX company in the Company information form, and this value could be used in the run() of the SysSetupFormRun
class. This would ensure that all forms are colored differently for
each Dynamics AX company to make sure users do not get lost among forms
when working in multi-company environments.